const LogModel = require('../../model/Log');
const moment = require('moment')
const Rules = require('../../model/Rules')

module.exports = {

    //获取日志列表
    changeRules: async (req, res, next) => {
        try {
            let rules = await Rules.findOne({ isDelete: 0 }).exec()
            console.log(rules)
            if (rules !== null) {
                rules.detail = req.body.detail
                await rules.save(
                    res.ok("规则修改成功")
                )
            } else {
                let newRules = new Rules({
                    detail: req.body.detail,
                    isDelete:0
                })
                await newRules.save()
                res.ok("规则创建成功", newRules)
            }
            // if (req.body.id) {
            //     let rules = await Rules.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            //     rules.detail = req.body.detail
            //     await rules.save(
            //         res.ok("规则修改成功")
            //     )
            // } else if (!req.body.id) {
            //     let newRules = new Rules({
            //         detail: req.body.detail
            //     })
            //     await newRules.save()
            //     res.ok("规则创建成功", newRules)
            // }
        } catch (e) {
            next(e)
        }
    },

    getRules: async (req, res, next) => {
        try {
            let rules = await Rules.findOne({isDelete: 0 }).exec()
            return res.ok('查询规则成功', rules)
        } catch (e) {
            next(e)
        }
    },

    deleteRules: async (req, res, next) => {
        try {
            let rules = await Rules.findOne({ _id: req.body.id, isDelete: 0 }).exec()
            rules.isDelete = 1
            await rules.save()
            return res.ok('删除规则成功', rules)
        } catch (e) {
            next(e)
        }
    },

};